<template>
  <view class="container">
    <view class="auth-illustration">
      <image src="/static/images/auth/illustration.svg" mode="aspectFit" />
    </view>
    <view class="login-box" :class="{ 'slide-enter-active': isPageShow }">
      <view class="welcome-text">欢迎回来</view>
      <view class="title">登录</view>
      <view class="form">
        <view class="form-item">
          <input
            class="input"
            type="text"
            v-model="username"
            placeholder="请输入用户名"
          />
        </view>
        <view class="form-item">
          <input
            class="input"
            type="password"
            v-model="password"
            placeholder="请输入密码"
          />
        </view>
        <button class="login-btn" @click="handleLogin">登录</button>
        <view class="register-link" @click="goToRegister">还没有账号？去注册</view>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import request from '@/utils/request'

const isPageShow = ref(false)

onMounted(() => {
  isPageShow.value = true
})

const username = ref('')
const password = ref('')

// 处理登录
const handleLogin = async () => {
  if (!username.value || !password.value) {
    uni.showToast({
      title: '请输入用户名和密码',
      icon: 'none'
    })
    return
  }

  try {
    const { data } = await request({
      url: '/api/auth/login',
      method: 'POST',
      data: {
        username: username.value,
        password: password.value
      }
    })

    // 保存token和用户信息
    uni.setStorageSync('token', data.token)
    uni.setStorageSync('userInfo', data)

    // 跳转到首页
    uni.reLaunch({
      url: '/pages/index/index'
    })
  } catch (error) {
    console.error('登录失败:', error)
    uni.showToast({
      title: '登录失败',
      icon: 'none'
    })
  }
}

// 跳转到注册页面
const goToRegister = () => {
  uni.navigateTo({
    url: '/pages/register/index'
  })
}
</script>

<style lang="scss" scoped>
.container {
  min-height: 100vh;
  background-color: #f8f8f8;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40rpx;
  box-sizing: border-box;
  position: relative;
  overflow: hidden;
}

.auth-illustration {
  width: 400rpx;
  height: 300rpx;
  margin-bottom: 40rpx;
  
  image {
    width: 100%;
    height: 100%;
  }
}

.welcome-text {
  font-size: 32rpx;
  color: #666;
  text-align: center;
  margin-bottom: 20rpx;
}

.login-box {
  width: 100%;
  max-width: 600rpx;
  background-color: #fff;
  border-radius: 24rpx;
  padding: 50rpx 40rpx;
  box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.08);
  transform: translateY(100rpx);
  opacity: 0;
  transition: all 0.6s ease;
  
  &.slide-enter-active {
    transform: translateY(0);
    opacity: 1;
  }
}

.title {
  font-size: 48rpx;
  font-weight: bold;
  text-align: center;
  margin-bottom: 80rpx;
  color: #333;
  letter-spacing: 2rpx;
}

.form-item {
  margin-bottom: 40rpx;
}

.input {
  width: 100%;
  height: 96rpx;
  background-color: #f5f5f5;
  border-radius: 48rpx;
  padding: 0 40rpx;
  font-size: 30rpx;
  color: #333;
  box-sizing: border-box;
  transition: all 0.3s ease;

  &:focus {
    background-color: #f0f0f0;
    box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.05);
  }
}

.login-btn {
  width: 100%;
  height: 96rpx;
  background-color: #018d71;
  color: #fff;
  border-radius: 48rpx;
  font-size: 34rpx;
  font-weight: bold;
  margin-top: 80rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: opacity 0.3s ease;

  &:active {
    opacity: 0.8;
  }
}

.register-link {
  text-align: center;
  font-size: 30rpx;
  color: #018d71;
  margin-top: 40rpx;
  transition: opacity 0.3s ease;

  &:active {
    opacity: 0.7;
  }
}
</style>